<template>
	<view class="container">
		<image src="/static/logo.png" class="logo" mode="aspectFit"></image>
		<view class="title">每日家访抽签系统</view>

		<!-- 小学部抽签区域 -->
		<view class="section">
			<view class="section-title">
				<uni-icons type="home" size="24" color="#3498db"></uni-icons>
				<text>小学部 (1-6年级)</text>
			</view>
			<view class="result-container">
				<view class="result-mask"></view>
				<scroll-view class="result-scroll" scroll-y :scroll-top="primaryScrollTop" scroll-with-animation
					:style="{height: scrollHeight + 'px'}">
					<view v-for="(student, index) in primaryStudents" :key="'p'+index" class="result-item"
						:class="{selected: student.selected}">
						{{ student.name }} ({{ student.grade }})
					</view>
				</scroll-view>
				<view class="result-indicator"></view>
			</view>
			<button class="btn primary" @click="drawPrimary" :disabled="isDrawingPrimary">
				{{ isDrawingPrimary ? '抽取中...' : '抽取小学部学生' }}
			</button>
		</view>

		<!-- 初中部抽签区域 -->
		<view class="section">
			<view class="section-title">
				<uni-icons type="building" size="24" color="#2ecc71"></uni-icons>
				<text>初中部 (7-9年级)</text>
			</view>
			<view class="result-container">
				<view class="result-mask"></view>
				<scroll-view class="result-scroll" scroll-y :scroll-top="secondaryScrollTop" scroll-with-animation
					:style="{height: scrollHeight + 'px'}">
					<view v-for="(student, index) in secondaryStudents" :key="'s'+index" class="result-item"
						:class="{selected: student.selected}">
						{{ student.name }} ({{ student.grade }})
					</view>
				</scroll-view>
				<view class="result-indicator"></view>
			</view>
			<button class="btn secondary" @click="drawSecondary" :disabled="isDrawingSecondary">
				{{ isDrawingSecondary ? '抽取中...' : '抽取初中部学生' }}
			</button>
		</view>

		<button class="btn reset" @click="reset">重置抽签</button>

		<!-- 彩色纸屑效果 -->
		<confetti ref="confetti" />

		<!-- 抽中提示框 -->
		<uni-popup ref="popup" type="center" :mask-click="false">
			<view class="popup-content">
				<view class="popup-title">🎉 恭喜 🎉</view>
				<view class="popup-text">{{ selectedStudentText }}</view>
				<button class="popup-btn" @click="closePopup">确定</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	// import Confetti from '@/components/Confetti.vue'

	export default {

		data() {
			return {
				scrollHeight: 120,
				primaryScrollTop: 0,
				secondaryScrollTop: 0,
				isDrawingPrimary: false,
				isDrawingSecondary: false,
				selectedStudentText: '',
				primaryStudents: [{
						name: "张紫柔",
						grade: "六年级",
						selected: false
					},
					{
						name: "付梦然",
						grade: "三年级",
						selected: false
					},

				],
				secondaryStudents: [{
					name: "李博睿",
					grade: "七年级",
					selected: false
				}]
			}
		},
		methods: {
			async drawPrimary() {
				if (this.isDrawingPrimary) return

				this.isDrawingPrimary = true

				// 重置所有选中状态
				this.primaryStudents.forEach(s => s.selected = false)

				// 等待下一次渲染更新按钮状态
				await this.$nextTick()

				// 滚动动画
				const duration = 3000
				const startTime = Date.now()
				const itemHeight = 60

				return new Promise((resolve) => {
					const animate = () => {
						const elapsed = Date.now() - startTime
						const progress = Math.min(elapsed / duration, 1)

						let easeProgress
						if (progress < 0.8) {
							easeProgress = progress * 1.25
						} else {
							easeProgress = 0.8 + (progress - 0.8) * 0.2
						}

						const totalHeight = this.primaryStudents.length * itemHeight
						const scrollDistance = totalHeight * 3 + (easeProgress * totalHeight)
						this.primaryScrollTop = scrollDistance % totalHeight

						if (progress < 1) {
							requestAnimationFrame(animate)
						} else {
							const randomIndex = Math.floor(Math.random() * this.primaryStudents.length)
							const selectedStudent = this.primaryStudents[randomIndex]

							this.primaryScrollTop = randomIndex * itemHeight
							selectedStudent.selected = true

							// 显示抽中提示
							this.showResultPopup(selectedStudent.name, selectedStudent.grade)

							// 播放庆祝效果
							this.playConfetti()

							// 确保状态正确恢复
							setTimeout(() => {
								this.isDrawingPrimary = false
								resolve()
							}, 0)
						}
					}

					animate()
				})
			},

			async drawSecondary() {
				if (this.isDrawingSecondary || this.secondaryStudents.length === 0) return

				this.isDrawingSecondary = true

				// 重置所有选中状态
				this.secondaryStudents.forEach(s => s.selected = false)

				// 等待下一次渲染更新按钮状态
				await this.$nextTick()

				// 滚动动画
				const duration = 3000
				const startTime = Date.now()
				const itemHeight = 60

				return new Promise((resolve) => {
					const animate = () => {
						const elapsed = Date.now() - startTime
						const progress = Math.min(elapsed / duration, 1)

						let easeProgress
						if (progress < 0.8) {
							easeProgress = progress * 1.25
						} else {
							easeProgress = 0.8 + (progress - 0.8) * 0.2
						}

						const totalHeight = this.secondaryStudents.length * itemHeight
						const scrollDistance = totalHeight * 3 + (easeProgress * totalHeight)
						this.secondaryScrollTop = scrollDistance % totalHeight

						if (progress < 1) {
							requestAnimationFrame(animate)
						} else {
							const randomIndex = Math.floor(Math.random() * this.secondaryStudents.length)
							const selectedStudent = this.secondaryStudents[randomIndex]

							this.secondaryScrollTop = randomIndex * itemHeight
							selectedStudent.selected = true

							// 显示抽中提示
							this.showResultPopup(selectedStudent.name, selectedStudent.grade)

							// 播放庆祝效果
							this.playConfetti()

							// 确保状态正确恢复
							setTimeout(() => {
								this.isDrawingSecondary = false
								resolve()
							}, 0)
						}
					}

					animate()
				})
			},

			playConfetti() {
				// 确保Confetti组件已加载
				this.$nextTick(() => {
					if (this.$refs.confetti && typeof this.$refs.confetti.play === 'function') {
						this.$refs.confetti.play()
					}
				})
			},

			showResultPopup(name, grade) {
				this.selectedStudentText = `${name}同学 (${grade})被抽中啦！`
				this.$refs.popup.open()
			},

			closePopup() {
				this.$refs.popup.close()
			},

			reset() {
				this.primaryScrollTop = 0
				this.secondaryScrollTop = 0
				this.primaryStudents.forEach(s => s.selected = false)
				this.secondaryStudents.forEach(s => s.selected = false)
				this.closePopup()
			}
		}
	}
</script>



<style lang="scss" scoped>
	.popup-content {
		background-color: white;
		padding: 40rpx;
		border-radius: 20rpx;
		width: 600rpx;
		text-align: center;
	}

	.popup-title {
		font-size: 40rpx;
		font-weight: bold;
		color: #e74c3c;
		margin-bottom: 30rpx;
	}

	.popup-text {
		font-size: 36rpx;
		color: #333;
		margin-bottom: 40rpx;
		line-height: 1.5;
	}

	.popup-btn {
		background-color: #3498db;
		color: white;
		border: none;
		padding: 15rpx 40rpx;
		font-size: 32rpx;
		border-radius: 50rpx;
		margin-top: 20rpx;
	}

	/* 样式保持不变，与之前相同 */
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx;
		background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
		min-height: 100vh;
	}

	.logo {
		width: 200rpx;
		height: 200rpx;
		margin-bottom: 40rpx;
		border-radius: 50%;
		box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
	}

	.title {
		font-size: 48rpx;
		font-weight: bold;
		color: #2c3e50;
		margin-bottom: 60rpx;
		text-shadow: 2rpx 2rpx 6rpx rgba(0, 0, 0, 0.1);
	}

	.section {
		width: 100%;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 40rpx;
		box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
	}

	.section-title {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
		color: #3498db;
		margin-bottom: 30rpx;

		text {
			margin-left: 15rpx;
		}
	}

	.result-container {
		position: relative;
		height: 180rpx;
		margin: 30rpx 0;
		overflow: hidden;
		border-radius: 15rpx;
		background-color: #f8f9fa;
		box-shadow: inset 0 0 15rpx rgba(0, 0, 0, 0.05);
	}

	.result-mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		height: 60rpx;
		background: linear-gradient(to bottom, rgba(248, 249, 250, 1) 0%, rgba(248, 249, 250, 0) 100%);
		z-index: 2;
	}

	.result-scroll {
		width: 100%;
		position: relative;
		z-index: 1;
	}

	.result-item {
		height: 60px;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		color: #666;

		&.selected {
			color: #1976d2;
			font-weight: bold;
			font-size: 36rpx;
		}
	}

	.result-indicator {
		position: absolute;
		top: 50%;
		left: 0;
		right: 0;
		height: 2px;
		background-color: #3498db;
		z-index: 2;
		transform: translateY(-1px);

		&::before,
		&::after {
			content: '';
			position: absolute;
			top: -8px;
			width: 0;
			height: 0;
			border-left: 10px solid transparent;
			border-right: 10px solid transparent;
			border-top: 10px solid #3498db;
		}

		&::before {
			left: 20px;
		}

		&::after {
			right: 20px;
		}
	}

	.btn {
		width: 80%;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 45rpx;
		font-size: 32rpx;
		color: white;
		margin: 30rpx auto;
		box-shadow: 0 8rpx 12rpx rgba(0, 0, 0, 0.1);
		transition: all 0.3s ease;

		&:active {
			transform: translateY(4rpx);
			box-shadow: 0 4rpx 6rpx rgba(0, 0, 0, 0.1);
		}

		&[disabled] {
			opacity: 0.7;
		}

		&.primary {
			background: linear-gradient(to right, #3498db, #2980b9);

			&:active {
				background: linear-gradient(to right, #2980b9, #2472a4);
			}
		}

		&.secondary {
			background: linear-gradient(to right, #2ecc71, #27ae60);

			&:active {
				background: linear-gradient(to right, #27ae60, #219653);
			}
		}

		&.reset {
			background: linear-gradient(to right, #e74c3c, #c0392b);

			&:active {
				background: linear-gradient(to right, #c0392b, #a93226);
			}
		}
	}
</style>